package com.pro01.backend.controller;

import com.pro01.backend.dto.CommentDTO;
import com.pro01.backend.exception.ForbiddenException;
import com.pro01.backend.pojo.PageResult;
import com.pro01.backend.pojo.Result;
import com.pro01.backend.service.Forum2CommentService;
import com.pro01.backend.utils.TokenUtil;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/forum2")
public class AdminForum2CommentController {

    @Autowired
    private Forum2CommentService commentService;

    // 管理员查看所有评论
    @GetMapping("/comments")
    public Result<PageResult<CommentDTO>> getAllComments(@RequestParam(defaultValue = "1") int pageNum,
                                                         @RequestParam(defaultValue = "10") int pageSize,
                                                         HttpServletRequest request) {
        String role = TokenUtil.getRole(request);
        if (!"ADMIN".equals(role)) {
            // return Result.error("无权限访问，仅限管理员");
            throw new ForbiddenException("无权限访问，仅限管理员");
        }

        PageResult<CommentDTO> result = commentService.getAllCommentsPaged(pageNum, pageSize);
        return Result.success(result);
    }

    // 管理员删除任意评论
    @DeleteMapping("/comments/{commentId}")
    public Result<Void> deleteCommentAsAdmin(@PathVariable Long commentId,
                                             HttpServletRequest request) {
        String role = TokenUtil.getRole(request);
        if (!"ADMIN".equals(role)) {
            // return Result.error("无权限操作，仅限管理员");
            throw new ForbiddenException("无权限访问，仅限管理员");
        }

        try {
            int result = commentService.deleteCommentAsAdmin(commentId);
            return result > 0 ? Result.success() : Result.error("删除失败或评论不存在");
        } catch (RuntimeException e) {
            return Result.error(e.getMessage());
        }
    }

    // 管理员隐藏任意评论
    @PutMapping("/comments/{commentId}/hide")
    public Result<Void> hideCommentAsAdmin(@PathVariable Long commentId,
                                           HttpServletRequest request) {
        if (!"ADMIN".equals(TokenUtil.getRole(request))) {
            // return Result.error("无权限，仅管理员可操作");
            throw new ForbiddenException("无权限访问，仅限管理员");
        }

        try {
            int result = commentService.updateCommentVisibilityAsAdmin(commentId, "HIDDEN");
            return result > 0 ? Result.success() : Result.error("隐藏失败或评论不存在");
        } catch (RuntimeException e) {
            return Result.error(e.getMessage());
        }
    }

    // 管理员显示任意评论
    @PutMapping("/comments/{commentId}/unhide")
    public Result<Void> unhideCommentAsAdmin(@PathVariable Long commentId,
                                             HttpServletRequest request) {
        if (!"ADMIN".equals(TokenUtil.getRole(request))) {
            // return Result.error("无权限，仅管理员可操作");
            throw new ForbiddenException("无权限访问，仅限管理员");
        }

        try {
            int result = commentService.updateCommentVisibilityAsAdmin(commentId, "ACTIVE");
            return result > 0 ? Result.success() : Result.error("显示失败或评论不存在");
        } catch (RuntimeException e) {
            return Result.error(e.getMessage());
        }
    }

}

